package com.wzy.ssm.login.controller;

import com.wzy.ssm.user.model.UserLogin;
import com.wzy.ssm.user.model.UserLoginExample;
import com.wzy.ssm.user.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class LoginController {

    private UserLoginService userLoginService;

    @Autowired
    public LoginController(UserLoginService userLoginService) {
        this.userLoginService = userLoginService;
    }

    //处理登录
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Object login(@RequestBody UserLogin user,HttpServletResponse response) {
        // 获取用户名和密码
        String userLoginId = user.getUserLoginId();
        String password = user.getCurrentPassword();
        // 从数据库中获取用户名和密码后进行判断
        if (userLoginId != null && password != null) {
            UserLoginExample example =new UserLoginExample();
            UserLoginExample.Criteria criteria = example.createCriteria();
            criteria.andUserLoginIdEqualTo(userLoginId);
            criteria.andCurrentPasswordEqualTo(password);
            List<UserLogin> list = userLoginService.selectByExample(example);
            //根据给定的用户名和密码查询
            //如果有结果，证明用户名和密码正确
            if (list.size() > 0) {
                UserLogin userLogin =list.get(0);
                Map loginInfo = new HashMap();
                loginInfo.put("loginStatus","200");
                loginInfo.put("userInfo",userLogin);

                return loginInfo;
            }
        }
        //登录失败，在前端登录用这个代码判断是否成功
        return "err400";
    }

}
